package de.hochschule.bremen.usea.server.service;

import java.util.Vector;

import de.hochschule.bremen.usea.commons.exceptions.LoginFailureException;
/*
 * usea, Copyright 2011 André König
 * Hochschule Bremen - University of Applied Sciences - All Rights Reserved.
 *
 * $Id: UserService.java 17 2011-07-06 06:51:01Z andre.koenig $
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * Contact:
 *     André König: akoenig@stud.hs-bremen.de
 * 
 * Web:
 *     http://tutorium-prog2.tumblr.com
 * 
 */
import de.hochschule.bremen.usea.commons.vo.User;

public class UserService {

	private static Vector<User> users = new Vector<User>();
	
	public UserService() {
		
		// Erstellung von Beispiel-Benutzern.
		// Diese kommen normalerweise aus dem Storage-System (Persistence).
		// Werden also aus einer Datenbank bzw. aus einer Datei gelesen.
		users.add(new User("andre.koenig@gmail.com", "test123"));
		users.add(new User("steve.jobs@apple.com", "apple4ever"));
		users.add(new User("larry@google.com", "gpluswillkillfacebook"));
	}
	
	/**
	 * Login.
	 * 
	 * Prueft, ob die Benutzerdaten des übergebenen Users mit denen des
	 * gespeicherten Benutzers übereinstimmen.
	 *
	 * @param wannabe Der Benutzer, welcher sich einloggen moechte.
	 *
	 * @throws LoginFailureException
	 */
	public void login(User wannabe) throws LoginFailureException {
		boolean success = false;
		
		search : for (User user : users) {
			if (user.getEmail().equals(wannabe.getEmail()) && user.getPassword().equals(wannabe.getPassword())) {
				success = true;
				
				break search;
			}
		}

		if (!success) {
			throw new LoginFailureException(wannabe);
		}
	}
}